<?php
/**
 * [商网通 System] Copyright swtshop.cn
 * QQ: 1170787711
 * Author: gj.name
 * This is NOT a freeware
*/
defined('IN_UQ') or exit('Access Denied');
class category_uqcms extends control
{
    function __construct()
    {
        $this->aid = $_SESSION['admin']['aid'];
        parent::__construct();
    }
    public function index()
    {
        $cid = isset($_GET['cid']) ? $_GET['cid'] : '0';
        $data = $this->db->get_all("select * from " . table('category') . " where pcid = " . $cid . " order by px_asc asc");
        $this->assign('data', $data);
        $this->display();
    }
    public function edit()
    {
        $cid = @$_GET['cid'] ? $_GET['cid'] : false;
        if ($cid) {
            $this->assign('row', $this->module('category')->get_one($cid));
        }
        $this->display('category/edit');
    }
    public function save()
    {
        if ($_POST) {
            $data['pcid'] = P('pcid', '', '0');
            $name = P('name', '分类名称不能为空');
            $data['keywords'] = P('keywords');
            $data['describe'] = P('describe');
            $data['images'] = P('images');
            $data['px_hot'] = P('px_hot', '', '0');
            $data['px_index'] = P('px_index', '', '0');
            $data['px_recommend'] = P('px_recommend', '', '0');
            $data['px_asc'] = P('px_asc', '', '0');
            $data['status'] = P('status', '', '0');
            $data['addtime'] = time();
            $cid = P('cid', false);
            if ($cid) {
                $data['name'] = $name;
                $query = $this->db->update(table('category'), $data, 'cid =' . $cid);
            } else {
                $name_arr = explode(',', $name);
                $count = 0;
                $loop = 0;
                $this->db->autocommit();
                foreach ($name_arr as $v) {
                    $data['name'] = $v;
                    $query = $this->db->add(table('category'), $data);
                    if ($query) {
                        $count++;
                    }
                    $loop++;
                }
                if ($count == $loop && $count > '0') {
                    $this->db->commit();
                    $query = true;
                } else {
                    $this->db->rollback();
                    $query = false;
                }
            }
            if ($query) {
                right_json();
            } else {
                error_json('保存失败');
            }
        } else {
            error_json('提交错误');
        }
    }
    public function del()
    {
        $cid = $_GET['cid'] ? $_GET['cid'] : showmsg('参数错误！');
        $row = $this->db->get_one("select cid,pcid from " . table('category') . " where pcid = " . $cid . " limit 1");
        if ($row) {
            if ($row['cid']) {
                showmsg('该分类下还有数据');
            } else {
                $query = $this->db->delete(table('category'), 'cid = ' . $cid);
                if ($query) {
                    showmsg('删除成功');
                } else {
                    showmsg('删除失败');
                }
            }
        } else {
            showmsg('操作失败');
        }
    }
    public function px_status()
    {
        $cid = isset($_POST['cid']) ? $_POST['cid'] : exit('cid error');
        $type = $_POST['type'];
        $this->db->query("update " . table('category') . " set {$type} = ({$type} + 1)%2 where cid = " . $cid . " limit 1");
        $row = $this->db->get_one("select * from " . table('category') . " where cid = " . $cid);
        echo $row[$type];
    }
    public function px_update()
    {
        $cid = isset($_POST['cid']) ? $_POST['cid'] : exit('id error');
        $px = isset($_POST['px']) ? $_POST['px'] : exit('px error');
        $query = $this->db->update(table('category'), array('px_asc' => $px), 'cid = ' . $cid);
        if ($query) {
            exit('1');
        } else {
            exit('2');
        }
    }
    public function ajax_get_next_list()
    {
        $pcid = isset($_POST['pcid']) ? $_POST['pcid'] : '0';
        $query = $this->db->get_all("select cid,pcid,name from " . table('category') . " where pcid = " . $pcid);
        if ($query) {
            foreach ($query as $k => $v) {
                $clist[] = $v;
            }
            if (isset($clist)) {
                $data['error'] = '0';
                $data['last'] = '0';
                $data['category'] = $clist;
                echo json_encode($data);
            } else {
                $data['error'] = '0';
                $data['last'] = '1';
                echo json_encode($data);
            }
        } else {
            $data['error'] = '0';
            $data['last'] = '1';
            echo json_encode($data);
        }
    }
    public function ajax_get_parents_list()
    {
        $cid = isset($_POST['cid']) ? $_POST['cid'] : error_json('提交错误');
        $cats_arr = $this->module('category')->get_parents_cid($cid);
        $cats_list = $this->module('category')->get_parents_list($cats_arr);
        if ($cats_list) {
            echo json_encode($cats_list);
        } else {
            error_json('获取失败');
        }
    }
}